package engine.asset
{
	import away3d.events.*;
	import engine.core.*;
	import engine.loaders.*;
	import game.asset.*;
	
	public class RxAssetAnimation extends RxAsset
	{
		private var m_loader3d:RxLoader3D;
		private static var s_type:int;
		
		public function RxAssetAnimation()
		{
			if (this.m_loader3d)
			{
				this.m_loader3d.Delete();
				this.m_loader3d = null;
			}
			super(s_type);
		}
		
		public static function Initialize() : void
		{
			s_type = RxAsset.RegisterType("animation", CreateFromXML);
		}
		
		public static function CreateFromXML(param1:XML, param2:String) : RxAssetAnimation
		{
			var _loc_3:* = param1.@name;
			var _loc_4:* = param1.@url;
			_loc_4 = RxUtilities.MergePath(param2, _loc_4);
			return Create(_loc_4, _loc_3);
		}
		
		public static function Create(param1:String, param2:String) : RxAssetAnimation
		{
			var _loc_3:* = GetByURL(param1);
			if (_loc_3 != null)
			{
				var _loc_4:* = _loc_3;
				var _loc_5:* = _loc_3.m_references + 1;
				_loc_4.m_references = _loc_5;
				return _loc_3;
			}
			_loc_3 = new RxAssetAnimation;
			_loc_3.SetURL(param1);
			_loc_3.SetName(param2);
			return _loc_3;
		}
		
		override public function Load() : void
		{
			super.Load();
			switch(m_state)
			{
				case STATE_EMPTY:
				{
					this.m_loader3d = new RxLoader3D();
					this.m_loader3d.addEventListener(LoaderEvent.RESOURCE_COMPLETE, this.OnLoadComplete, m_source == RxAsset.SOURCE_REMOTE);
					this.m_loader3d.addEventListener(LoaderEvent.LOAD_ERROR, this.OnLoadError);
					this.m_loader3d.load(m_url, RxLoader.GetAssetPath(), new RxRUMParser(s_worker));
					m_state = STATE_LOADING;
					break;
				}
				case STATE_LOADING:
				{
					break;
				}
				case STATE_ERROR:
				{
					ExecuteCallbacks(false);
					break;
				}
				case STATE_READY:
				{
					ExecuteCallbacks(true);
					break;
				}
				default:
				{
					break;
				}
			}
		}
		
		private function OnLoadComplete(event:LoaderEvent) : void
		{
			this.m_loader3d.removeEventListener(LoaderEvent.RESOURCE_COMPLETE, this.OnLoadComplete);
			this.m_loader3d.removeEventListener(LoaderEvent.LOAD_ERROR, this.OnLoadError);
			m_size = this.m_loader3d.size;
			if (this.m_loader3d.mesh != null)
			{
				m_state = STATE_READY;
				ExecuteCallbacks(true);
			}
			else
			{
				m_state = STATE_ERROR;
				ExecuteCallbacks(false);
			}
		}
		
		private function OnLoadError(event:LoaderEvent) : void
		{
			this.m_loader3d.removeEventListener(LoaderEvent.RESOURCE_COMPLETE, this.OnLoadComplete);
			this.m_loader3d.removeEventListener(LoaderEvent.LOAD_ERROR, this.OnLoadError);
			m_state = STATE_ERROR;
			ExecuteCallbacks(false);
		}
		
		public static function GetByURL(param1:String) : RxAssetAnimation
		{
			return RxAssetAnimation(_getByURL(param1, s_type));
		}
	}
}